<template>
  <div class="company-step" v-loading="loading">
    <companyStep ref="steps" :bussType="bussType" :id="stepId" @loading="handleLoading" />
  </div>
</template>
<script>
import companyStep from './components/CompanyStep.vue';
import userApi from '@/api/user';
import businessApi from '@/api/business';
import { decrypt } from '@/utils/sms4util';

export default {
  props: {
    key: {
      type: String,
    },
    bussType: {
      default: '5',
    },
  },
  components: {
    companyStep,
  },
  created() {
    this.initData();
  },
  data() {
    return {
      userData: {},
      stepId: null,
      loading: false,
    };
  },
  methods: {
    async initData() {
      // 判断业务类型

      this.loading = true;
      if (!businessApi.isCompanyBussType(this.bussType)) {
        this.$alert('请正确打开页面', '提示', {
          showClose: false,
          showConfirmButton: false,
          type: 'warning',
          center: true,
        });
        return false;
      }
      if (this.key) {
        // 通过token登录
        const res = await userApi.loginWithToken({ token: this.key });
        const { token, sysAllDictItems, userInfo } = res;
        const decryptUserInfo = {
          ...userInfo,
          unitTel: userInfo.unitTel ? decrypt(userInfo.unitTel) : '',
          creditCode: userInfo.creditCode ? decrypt(userInfo.creditCode) : '',
        }
        this.$store.commit('dict/setDict', sysAllDictItems);
        this.$store.commit('user/setToken', token);
        this.$store.commit('user/setUserInfo', decryptUserInfo);
        // 判断用户类型
        if (userInfo.userType != 2) {
          this.$alert('请正确打开页面', '提示', {
            showClose: false,
            confirmButtonText: '跳转',
            type: 'warning',
            center: true,
            callback: () => {
              this.$router.push({
                path: '/dockPersonalApplyStep',
                query: {
                  bussType: this.bussType,
                  key: this.key
                },
              });
            },
          });
          return false;
        }
        // 判断用户是否已经完善信息
        await this.getUserData();
        this.loading = false;
      } else {
        this.$alert('请正确打开页面', '提示', {
          showClose: false,
          showConfirmButton: false,
          type: 'warning',
          center: true,
        });
      }
    },
    async getUserData() {
      let params = {
        id: this.$store.state.user.userInfo.id,
        type: this.$store.state.user.userInfo.userType,
      };
      // 获取用户资质
      const res = await userApi.getUserInfo(params);
      this.userData = {
        ...res,
        unitTel: res.unitTel ? decrypt(res.unitTel) : '',
        creditCode: res.creditCode ? decrypt(res.creditCode) : '',
      };
      const userQua = res?.sysUnitQua;
      this.$store.commit('user/setUserQua', userQua);
      if (userQua) {
        let disabled = [];
        if (userQua.ytCode) {
          disabled.push('1');
        }
        if (userQua.hltCode) {
          disabled.push('2');
        }
        if (userQua.jsjgCode) {
          disabled.push('3');
        }
        if (userQua.jxdqCode) {
          disabled.push('4');
        }
        if (userQua.lcCode) {
          disabled.push('5');
        }
        if (disabled.length >= 5 && businessApi.isRegBussType(this.bussType)) {
          this.$alert('已有全部资质', '提示', {
            showClose: false,
            showConfirmButton: false,
            type: 'warning',
            center: true,
          });
        }
      }
      // 查询当前用户是否有申请
      await this.checkStatus();
      // 判断用户是否已经完善信息
      if (this.$store.state.user.userInfo.isCompleteInfo != 1) {
        this.$alert('请先完善单位信息', '提示', {
          confirmButtonText: '确定',
          showClose: false,
          type: 'warning',
          center: true,
          callback: () => {
            this.$router.push('/dockCompanyUserInfo');
          },
        });
        return false;
      }
      if (this.userData.regCert) {
        this.$alert('已有注册证书', '提示', {
          showClose: false,
          showConfirmButton: false,
          type: 'warning',
          center: true,
        });
        return false;
      }
    },
    async checkStatus() {
      let params = {
        bussType: this.bussType === '6' ? '5,6' : this.bussType,
        status: '0,1,3,9',
      };
      const { records } = await businessApi.getStepsList(params);
      if (records[0]) {
        let data = records[0];
        if (data.status == 0) {
          this.stepId = data.id;
          //已有申请
        } else if (data.status == 1 || data.status == 3) {
          this.$alert('当前申请正在受理审核中', '提示', {
            showClose: false,
            showConfirmButton: false,
            type: 'warning',
            center: true,
          });
        } else if (data.status == 9) {
          //已有申请-需补正
          this.$alert('当前申请需前往补正', '提示', {
            confirmButtonText: '确定',
            showClose: false,
            type: 'warning',
            center: true,
            callback: () => {
              this.$router.push({
                path: '/dockCompanyProcess',
                query: {
                  id: data.id,
                  bussType: this.bussType,
                  disabled: true,
                },
              });
            },
          });
        }
      } else {
        // 是否使用纸质证照
        if (businessApi.isCompanyBussType(this.bussType)
            && !businessApi.isRegBussType(this.bussType)
            && !this.$store.state.user.infoChecked
            && !this.$store.state.user.userQua
        ) {
          this.$alert("系统内未发现电子证照信息，是否有纸质证照进行申请", '提示', {
            confirmButtonText: '确定',
            showClose: false,
            type: 'warning',
            center: true,
            callback: () => {
              this.$store.state.user.izStock = true;
              this.$store.state.user.infoChecked = true;
              if (this.$store.state.user.userInfo.isCompleteInfo != 1) {
                this.$router.push('/dockCompanyUserInfo');
              }
            },
          });
        }
      }
    },
    // 加载中
    handleLoading(status) {
      this.loading = status;
    }
  },
};
</script>
<style lang="scss" scoped>
.company-step {
  margin: 20px auto;
  width: 1620px;
}
</style>
